home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 42
/
Amiga Format AFCD42 (Issue 126, Aug 1999).iso
/
-serious-
/
programming
/
other
/
jikes
/
src
/
depend.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1999-05-14
|
16KB
|
480 lines
// $Id: depend.cpp,v 1.6 1999/03/10 19:59:21 shields Exp $
//
// This software is subject to the terms of the IBM Jikes Compiler
// License Agreement available at the following URL:
// http://www.ibm.com/research/jikes.
// Copyright (C) 1996, 1998, International Business Machines Corporation
// and others. All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
#include "config.h"
#include <time.h>
#include "control.h"
#include "ast.h"
#include "semantic.h"
//
// Note that the types are ordered based on on the subtype relationship. We reverse
// the order here because the desired order for processing is the supertype relationship.
//
inline void TypeCycleChecker::ReverseTypeList()
{
int head,
tail;
for (head = 0, tail = type_list.Length() - 1; head < tail; head++, tail--)
{
TypeSymbol *temp = type_list[head];
type_list[head] = type_list[tail];
type_list[tail] = temp;
}
return;
}
void TypeCycleChecker::PartialOrder(Tuple<Semantic *> &semantic, int start)
{
type_list.Reset();
//
// assert that the "index" of all types that should be checked is initially set to OMEGA
//
for (int i = start; i < semantic.Length(); i++)
{
Semantic *sem = semantic[i];
for (int k = 0; k < sem -> compilation_unit -> NumTypeDeclarations(); k++)
{
AstClassDeclaration *class_declaration = sem -> compilation_unit -> TypeDeclaration(k) -> ClassDeclarationCast();
AstInterfaceDeclaration *interface_declaration = sem -> compilation_unit -> TypeDeclaration(k) -> InterfaceDeclarationCast();
if (class_declaration || interface_declaration)
{
SemanticEnvironment *env = (class_declaration ? class_declaration -> semantic_environment
: interface_declaration -> semantic_environment);
if (env) // type was successfully compiled thus far?
{
TypeSymbol *type = env -> Type();
if (type -> index == OMEGA)
ProcessSubtypes(type);
}
}
}
}
ReverseTypeList();
return;
}
void TypeCycleChecker::PartialOrder(SymbolSet &types)
{
//
// assert that the "index" of all types that should be checked is initially set to OMEGA
//
for (TypeSymbol *type = (TypeSymbol *) types.FirstElement(); type; type = (TypeSymbol *) types.NextElement())
{
if (type -> index == OMEGA)
ProcessSubtypes(type);
}
ReverseTypeList();
return;
}
void TypeCycleChecker::ProcessSubtypes(TypeSymbol *type)
{
stack.Push(type);
int indx = stack.Size();
type -> index = indx;
type -> subtypes_closure = new SymbolSet;
type -> subtypes_closure -> Union(*(type -> subtypes));
for (TypeSymbol *subtype = (TypeSymbol *) type -> subtypes -> FirstElement();
subtype;
subtype = (TypeSymbol *) type -> subtypes -> NextElement())
{
if (subtype -> index == OMEGA)
ProcessSubtypes(subtype);
type -> index = Min(type -> index, subtype -> index);
type -> subtypes_closure -> Union(*(subtype -> subtypes_closure));
}
if (type -> index == indx)
{
TypeSymbol *scc_subtype;
do
{
scc_subtype = stack.Top();
scc_subtype -> index = INFINITY;
*(scc_subtype -> subtypes_closure) = *(type -> subtypes_closure);
type_list.Next() = scc_subtype;
stack.Pop();
} while (scc_subtype != type);
}
return;
}
ConstructorCycleChecker::ConstructorCycleChecker(AstClassBody *class_body)
{
for (int k = 0; k < class_body -> NumConstructors(); k++)
{
AstConstructorDeclaration *constructor_declaration = class_body -> Constructor(k);
if (constructor_declaration -> index == OMEGA)
CheckConstructorCycles(constructor_declaration);
}
return;
}
void ConstructorCycleChecker::CheckConstructorCycles(AstConstructorDeclaration *constructor_declaration)
{
stack.Push(constructor_declaration);
int indx = stack.Size();
constructor_declaration -> index = indx;
AstConstructorDeclaration *called_constructor_declaration = NULL;
AstConstructorBlock *constructor_block = constructor_declaration -> constructor_body;
if (constructor_block -> explicit_constructor_invocation_opt)
{
AstThisCall *this_call = constructor_block -> explicit_constructor_invocation_opt -> ThisCallCast();
MethodSymbol *called_constructor = (MethodSymbol *) (this_call ? this_call -> symbol : NULL);
if (called_constructor)
{
called_constructor_declaration = (AstConstructorDeclaration *) called_constructor -> method_or_constructor_declaration;
if (called_constructor_declaration -> index == OMEGA)
CheckConstructorCycles(called_constructor_declaration);
constructor_declaration -> index = Min(constructor_declaration -> index, called_constructor_declaration -> index);
}
}
if (constructor_declaration -> index == indx)
{
//
// If the constructor_declaration is alone in its strongly connected component (SCC),
// and it does not form a trivial cycle with itsself, pop it, mark it and return;
//
if (constructor_declaration == stack.Top() && constructor_declaration != called_constructor_declaration)
{
stack.Pop();
constructor_declaration -> index = INFINITY;
}
//
// Otherwise, all elements in the stack up to (and including) constructor_declaration form an SCC.
// Pop them off the stack, in turn, mark them and issue the appropriate error message.
//
else
{
do
{
called_constructor_declaration = stack.Top();
stack.Pop();
called_constructor_declaration -> index = INFINITY;
constructor_block = (AstConstructorBlock *) called_constructor_declaration -> constructor_body;
AstMethodDeclarator *constructor_declarator = called_constructor_declaration -> constructor_declarator;
Semantic *sem = called_constructor_declaration -> constructor_symbol
-> containing_type -> semantic_environment -> sem;
sem -> ReportSemError(SemanticError::CIRCULAR_THIS_CALL,
constructor_block -> explicit_constructor_invocation_opt -> LeftToken(),
constructor_block -> explicit_constructor_invocation_opt -> RightToken(),
sem -> lex_stream -> Name(constructor_declarator -> identifier_token));
} while (called_constructor_declaration != constructor_declaration);
}
}
return;
}
//
// assert that the "index" of all types that should be checked is initially set to OMEGA
//
void TypeDependenceChecker::PartialOrder()
{
for (FileSymbol *file_symbol = (FileSymbol *) file_set.FirstElement();
file_symbol;
file_symbol = (FileSymbol *) file_set.NextElement())
{
for (int j = 0; j < file_symbol -> types.Length(); j++)
{
TypeSymbol *type = file_symbol -> types[j];
if (type -> incremental_index == OMEGA)
ProcessType(type);
}
}
for (int k = 0; k < type_trash_bin.Length(); k++)
{
TypeSymbol *type = type_trash_bin[k];
if (type -> incremental_index == OMEGA)
ProcessType(type);
}
return;
}
void TypeDependenceChecker::ProcessType(TypeSymbol *type)
{
stack.Push(type);
int indx = stack.Size();
type -> incremental_index = indx;
type -> dependents -> RemoveElement(type); // if dependents is reflexive make it non-reflexive - saves time !!!
type -> dependents_closure = new SymbolSet;
type -> dependents_cl